package com.mask.bank.service.impl;

import com.mask.bank.dao.AccountDao;
import com.mask.bank.entity.AccountEntity;
import com.mask.bank.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ClassName AccountServiceImpl
 * Create by hx
 * Date 2021/9/16 11:26 上午
 */

@Service
//@Transactional
public class AccountServiceImpl implements AccountService {

    private final Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Autowired
    private AccountDao accountDao;

    @Override
    public void transForMoney(AccountEntity outAccount, AccountEntity inAccount, Double money) {
        logger.info("开始转账中...");
        try {
            int outFlag = accountDao.outMoney(outAccount.getName(), outAccount.getCardNumbers(), money);
            logger.info("[{}]账户银行卡号[{}]转出[{}]元",outAccount.getName(),outAccount.getCardNumbers(),money);
            int inFlag = accountDao.inMoney(inAccount.getName(), inAccount.getCardNumbers(), money);
            logger.info("[{}]账户银行卡号[{}]转入[{}]元",inAccount.getName(),inAccount.getCardNumbers(),money);
        }catch (Exception e){
            logger.warn("转账异常!!!");
        }
        logger.info("转账结束---");
    }

    @Override
    public void addAccount(AccountEntity accountEntity) {
        Integer flag = accountDao.addAccount(accountEntity);
        if (flag != null && flag == 1){
            logger.info("添加账户[{}]成功,银行卡号为[{}],账户金额为[{}]",
                    accountEntity.getName(),accountEntity.getCardNumbers(),accountEntity.getMoney());
        }else {
            logger.warn("开户异常!!!");
        }
    }

    @Override
    public void deleteAccount(AccountEntity deleteAccount) {
        Integer flag = accountDao.deleteAccount(deleteAccount);
        if (flag != null && flag == 1){
            logger.info("删除银行卡号为[{}]的账户[{}]成功",deleteAccount.getCardNumbers(),deleteAccount.getName());
        }else {
            logger.warn("删除账户[{}]操作异常!!",deleteAccount.getName());
        }
    }

    @Override
    public AccountEntity queryAccountMoney(AccountEntity queryAccountMoney) {
        AccountEntity accountEntity = new AccountEntity();
        try {
            accountEntity = accountDao.queryAccountMoney(queryAccountMoney);
            logger.info("查询账户[{}]卡号[{}]余额为[{}]",queryAccountMoney.getName(),
                    queryAccountMoney.getCardNumbers(),accountEntity.getMoney());
        }catch (Exception e){
            logger.warn("查询异常！！！");
        }
        return accountEntity;
    }
}
